<?php

class Con_up extends Acan{
	
	public $defaultAct='index';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->id = "anmp";
	}
	function Act_index(){
		include $this->Atpl();
	}
	function Act_update(){
		$type = $this->id;
		$anmp = Afun_new('anmp');
		$lists = $anmp->find_list('acan').$anmp->find_list($type);
		$anmp->md5_list($lists,'','update_'.$type);
		$Aobj_new = unserialize(file_get_contents(Acan_anmp_update_url));
		Afun_filecache_set('update_new_'.$type,$Aobj_new,0,'./adata/up/');
		$Aobj = Afun_filecache_get('update_'.$type,'./adata/up/');
		if($Aobj_new->version_c > $Aobj->version_c){
			$msg = '控制台发现新版本 v'.$Aobj_new->version_c.' <a href="'.$this->Amodr.'-update_2-'.$type.'">点击更新</a>';
		}else{
			$msg = '你目前使用是最新版本,无需更新 <a href="'.$this->Amodr.'-update_2-'.$type.'">点击检测文件</a>';
		}
		echo $msg;
	}
	function Act_update_2(){
		$type = $this->id;
		$Aobj_new = Afun_filecache_get('update_new_'.$type,'./adata/up/');
		$Aobj = Afun_filecache_get('update_'.$type,'./adata/up/');
		foreach ($Aobj_new->files as $key => $val) {
			if(empty($Aobj->files[$key])){
				$Achg[] = $key;//新加
				$msg_up .= $key.'<br>';
			}elseif($Aobj->files[$key]!=$val){
				$Achg[] = $key;//修改
				$msg_up .= $key.'<br>';
			}
		}
		$Adata->files = $Achg;
		$Adata->num = count($Achg);
		if($Adata->num == 0){echo '需要更新0个文件';}else{
			echo '总共需要更新'.$Adata->num.'个文件';
			echo '<hr>';
			echo '对比完成 <a href="'.$this->Amodr.'-update_3-'.$type.'">开始更新</a>';
			echo '<hr>';
			echo $msg_up;
		}
		Afun_filecache_set('update_chg_'.$type,$Adata);
	}
	function Act_update_3(){
		$type = $this->id;
		$upurl = Acan_anmp_update_urlpath;
		$path = A_ROOT.'./temp/update/';
		$Adata = Afun_filecache_get('update_chg_'.$type);
		if(empty($Adata->files)){Afun_refresh('',$this->Amodr.'-update-'.$type);}
		foreach($Adata->files as $k=>$v){
			if(!file_exists(dirname($path.$v))){Afun_file_mkdirm(dirname($path.$v));}
			if(file_exists($path.$v)){unlink($path.$v);}
			file_put_contents($path.$v,file_get_contents($upurl.$v));
			unset($Adata->files[$k]);
			$Adata->have = count($Adata->files);
			Afun_filecache_set('update_chg_'.$type,$Adata);
			echo '已下载'.$Adata->num - $Adata->have.'个文件,还剩'.$Adata->have.'个文件';
			Afun_refresh('',$this->Amodr.'-update_4-'.$type);
		}
	}
	function Act_update_4(){
		$type = $this->id;
		$prr = explode(':',Acan_anmp_path);
		$cmd = $prr[0].': && xcopy '.A_ROOT.'temp\update '.Acan_anmp_path.'\ /E /Y';
		echo Afun_run_cmd($cmd);
		Afun_file_rmdirm(A_ROOT.'temp\update\\'); 
		Afun_refresh('升级成功',$this->Amodr.'-index');
	}

}

?>